
//
//  UIImageView+load.m
//  UseData
//
//  Created by Lenny on 16/5/17.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "UIImageView+load.h"
#import "ImageManager.h"

@implementation UIImageView (load)

- (void)loadUrl:(NSString *)strUrl {

    NSString *name = [[ImageManager shareManager] getNameWithStr:strUrl];
    BOOL isHas = [[ImageManager shareManager] isCheck:name];
    if (isHas == YES) {
        //已存在
        NSString *path = [[ImageManager shareManager] findFilePath];
        NSString *fullPath = [NSString stringWithFormat:@"%@/%@",path,name];
        self.image = [UIImage imageWithContentsOfFile:fullPath];
        return;
    }
    
    //不存在
    NSURL *url = [NSURL URLWithString:strUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        UIImage *image = [UIImage imageWithData:data];
        [[ImageManager shareManager] saveWithImage:image withName:strUrl];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.image = image;
        });
    }];
    [dataTask resume];
}

@end
